package com.apes.report.ext;

import com.bstek.ureport.cache.ReportDefinitionCache;
import com.bstek.ureport.definition.ReportDefinition;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Controller
public class ApesMemoryReportDefinitionCache implements ReportDefinitionCache {
    private Map<String,ReportDefinition> reportMap= new ConcurrentHashMap<>();

    @Override
    public ReportDefinition getReportDefinition(String file) {
        return reportMap.get(file);
    }
    @Override
    public void cacheReportDefinition(String file,ReportDefinition reportDefinition) {
        if(reportMap.containsKey(file)){
            reportMap.remove(file);
        }
        reportMap.put(file, reportDefinition);
    }

    @GetMapping("/clearAllCacheReportDefinition")
    @ResponseBody
    public String clearAllCacheReportDefinition(){
        if (reportMap != null) {
            reportMap.clear();
        }
        return "清除打印报表成功！";
    }
}
